(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.monitor.wfTxnProcess.viewController', viewController);
	viewController.$inject = [ '$scope', '$q', '$filter', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService' ];
	function viewController($scope, $q, $filter, Constants, gridService, modalService, layoutConfig, validatorConfig, authService) {
		var ctrl = this, monitorInfo = $scope.$stateParams.params, log = $scope.loggerFactory.getLogger("wfTxnProcessView");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.monitor = {};

		ctrl.init = function() {
			ctrl.wfTaskInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.wfTask.tableConfigs.table, wfTaskRowSelectionChanged);
			ctrl.wfTaskInnerGridOptions.data = 'vm.monitor.wfTasks';
			function wfTaskRowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedRows = selectedRows;
				if (ctrl.selectedRows && ctrl.selectedRows.length > 0) {
					if (ctrl.selectedRows[0].taskStatus == 'Ready') {
						ctrl.showBtnUnclaimProcess = 'N';
						ctrl.showBtnReassignProcess = 'Y';
					} else if (ctrl.selectedRows[0].taskStatus == 'Reserved' || ctrl.selectedRows[0].taskStatus == 'InProgress') {
						ctrl.showBtnUnclaimProcess = 'Y';
						ctrl.showBtnReassignProcess = 'Y';
					} else if (ctrl.selectedRows[0].taskStatus == 'Completed') {
						ctrl.showBtnUnclaimProcess = 'N';
						ctrl.showBtnReassignProcess = 'N';
					}
				}
			}
			
			var params = {
					instanceId : monitorInfo.instanceId
			};
			authService.view(params).then(function(response) {
				ctrl.monitor = response.data.monitor || {};
				ctrl.monitor.wfTasks = ctrl.monitor.wfTasks || [];
			});
		};

		ctrl.unclaim = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			authService.unclaim(ctrl.selectedRows[0]).then(function(_response) {
				var params = {
						instanceId : monitorInfo.instanceId
				};
				authService.view(params).then(function(response) {
					ctrl.monitor = response.data.monitor || {};
					ctrl.monitor.wfTasks = ctrl.monitor.wfTasks || [];
				});
			}, angular.noop);
			gridService.removeSelectedRows($scope);
		};
		
		ctrl.completeCustomTask = function() {
			modalService.openPromptDialogs({
				'title' : 'lable.monitor.taskresult',
				'btnCallback' : {
					'ok' : function(taskResult) {
						var requestData = {
							'instanceId' : ctrl.monitor.instanceId,
							'taskId' : ctrl.selectedRows[0].taskId,
							'taskResult' : taskResult
						};
						authService.completeCustomTask(requestData).then(completeSuccessful, completeFailed);
					}
				}
			});
			gridService.removeSelectedRows($scope);
		};
		
		function completeSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.complete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('apf.monitor.wfSetupProcess.list');
					}
				}
			});
		}
		function completeFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		ctrl.reassign = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			var _params = ctrl.selectedRows[0];
			modalService.openCustomDialogs({
			   'title': 'User Lookup',
			   'dialogsModule' : 'cda.user',
			   'params': _params,
			   'btnCallback' : {
				   'ok' : function(users) {							
						if (users && users.length > 0) {
							var __params = {};
							__params.assignedUser = $filter('encode')(users[0].userCode);
							__params.taskId = _params.taskId;
							authService.reassign(__params).then(function(_response) {
								var params = {
										instanceId : monitorInfo.instanceId
								};
								authService.view(params).then(function(response) {
									ctrl.monitor = response.data.monitor || {};
									ctrl.monitor.wfTasks = ctrl.monitor.wfTasks || [];
								});
							}, angular.noop);
						}
					}
				}		
			});
			//gridService.removeSelectedRows($scope);
		};

	}
})(angular);